<?php
namespace app\model;


use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class GoodsModel extends BaseModel{

    protected $table = 'rl_goods';

    //关联图片组
    public function goodsImages(): HasMany
    {
        return $this->hasMany('app\model\GoodsImagesModel','goods_id','id')->where('status',1)->orderBy('sort','desc');
    }

    //关联营期
    public function goodsPeriods(): HasMany
    {
        return $this->hasMany('app\model\GoodsPeriodModel','goods_id','id')->where('status',1)->orderBy('goods_period_date','asc');
    }

    public function assemblyLocation(): HasOne{
        return $this->hasOne('app\model\AssemblyLocationModel','id','assembly_location_id')->where('status',1);
    }

    //关联规格
    public function goodsSpecifications(): HasMany
    {
        return $this->hasMany('app\model\GoodsSpecificationModel','goods_id','id')->where('status',1);
    }

    //活动状态
    public function getGoodsEndStatusAttribute($value): int
    {
        if (!$this->goods_end_time){
            return 1;
        }
        return ($this->goods_end_time>time())?1:2;
    }

    //商品标签
    public function getGoodsLabelsAttribute($value): array
    {
        if (!$value){
            return [];
        }
        return explode('|',$value);
    }

}